package com.mmall.service.impl;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by root on 18-4-7.
 */
@Service
public class CategoryServiceImpl implements ICategoryService{

    @Autowired
    private CategoryMapper categoryMapper;


    public ServerResponse getChildrenParallelCategory(Integer categoryId){
        if (categoryId != null) {
            List rs = categoryMapper.getChildrenParallelCategory(categoryId);
            if (rs == null || rs.size() == 0) {
                return ServerResponse.createByError("not have children");
            } else {
                return ServerResponse.createBySuccess(rs);
            }
        } else {
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
    }

    public ServerResponse addCategory(String categoryName, Integer parentId){
        if (StringUtils.isBlank(categoryName)){
            return ServerResponse.createByError("categoryName can not be blank");
        }
        if (parentId == null) {
            parentId = 0;
        }
        Category newCate = new Category();
        newCate.setParentId(parentId);
        newCate.setName(categoryName);
        newCate.setStatus(Const.Status.STATUS_VALID);
        int rs = categoryMapper.insert(newCate);
        if (rs > 0) {
            return ServerResponse.createBySuccess();
        } else {
            return ServerResponse.createByError("add Category failed, please try again");
        }
    }

    public ServerResponse setCategoryName(String categoryName, Integer categoryId){
        if (StringUtils.isBlank(categoryName)){
            return ServerResponse.createByError("categoryName can not be blank");
        }
        if (categoryId == null){
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Category updCate = new Category();
        updCate.setId(categoryId);
        updCate.setName(categoryName);
        int rs = categoryMapper.updateByPrimaryKeySelective(updCate);
        if (rs > 0) {
            return ServerResponse.createBySuccess();
        } else {
            return ServerResponse.createByError("update Category Name failed, please try again");
        }
    }


    public ServerResponse getDeepCategory(Integer categoryId){
        if (categoryId == null) {
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Set<Category> allChildrenCategories = new HashSet<Category>();
        getDeepChildrenCategory(allChildrenCategories, categoryId);
        return ServerResponse.createBySuccess(allChildrenCategories);
    }

    private void getDeepChildrenCategory(Set<Category> allChildrenCategories, Integer categoryId){
        if (categoryId == null) {
            return ;
        }
        ServerResponse<List<Category>> resp = this.getChildrenParallelCategory(categoryId);
        if (resp.isSuccess()){
            List<Category> data = resp.getData();
            allChildrenCategories.addAll(data);
            for (Category cate : data) {
                getDeepChildrenCategory(allChildrenCategories, cate.getId());
            }
        }
    }
}
